package backtracking.leetcode.medium;

import java.util.ArrayList;
import java.util.List;

/**
 * @author bruin_du
 * @description 允许重复选择元素的组合
 * @date 2022/9/5 23:17
 **/
public class OfferII081_CombinationSum {
    List<List<Integer>> lists = new ArrayList<>();
    List<Integer> list = new ArrayList<>();
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        recursion(candidates, target, 0);
        return lists;
    }

    private void recursion(int[] candidates, int target, int index) {
        if (target < 0 || index == candidates.length) {
            return;
        } else if (target == 0) {
            lists.add(new ArrayList<>(list));
            return;
        }
        list.add(candidates[index]);
        recursion(candidates, target - candidates[index], index);
        list.remove(list.size() - 1);
        recursion(candidates, target, index + 1);
    }
}
